package com.llxy.web.controller.worker;
import com.llxy.common.annotation.Log;
import com.llxy.common.core.controller.BaseController;
import com.llxy.common.core.domain.AjaxResult;
import com.llxy.common.core.page.TableDataInfo;
import com.llxy.common.enums.BusinessType;
import com.llxy.worker.domain.News;
import com.llxy.worker.dto.NewsDTO;
import com.llxy.worker.service.NewsService;
import com.llxy.worker.service.impl.NewsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* (t_news)表控制层
*
* @author 马瑞龙
*/
@RestController
@RequestMapping("/worker/news")
public class NewsController extends BaseController {
    /**
    * 服务对象
    */
    @Autowired
    private NewsService newsService;

    /**
     * 获取新闻列表
     */
    @PreAuthorize("@ss.hasPermi('worker:news:list')")
    @GetMapping("/list")
    @ApiOperation(httpMethod = "GET", value = "获取新闻列表", notes = "获取新闻列表")
    public TableDataInfo list(News news)
    {
        startPage();
        List<NewsDTO> categoryList = newsService.selectList(news);
        return getDataTable(categoryList);
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('worker:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id)
    {
        return success(newsService.selectByPrimaryKey(id));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('worker:news:add')")
    @Log(title = "新增新闻", businessType = BusinessType.INSERT)
    @ApiOperation(httpMethod = "POST", value = "新增新闻", notes = "新增新闻")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody News news)
    {
        news.setCreateBy(getUsername());
        return toAjax(newsService.insertSelective(news));
    }

    /**
     * 修改新闻的置顶状态
     */
    @PreAuthorize("@ss.hasPermi('worker:news:edit')")
    @Log(title = "修改新闻", businessType = BusinessType.UPDATE)
    @ApiOperation(httpMethod = "PUT", value = "修改新闻", notes = "修改新闻")
    @PutMapping("/updateIsTop/{id}/{topVal}")
    public AjaxResult editIsTop(@PathVariable("id") Integer id,@PathVariable Integer topVal)
    {
        News news = new News();
        news.setUpdateBy(getUsername());
        news.setId(id);
        news.setIsTop(topVal);
        return toAjax(newsService.updateByPrimaryKeySelective(news));
    }


    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('worker:news:edit')")
    @Log(title = "修改新闻", businessType = BusinessType.UPDATE)
    @ApiOperation(httpMethod = "PUT", value = "修改新闻", notes = "修改新闻")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody News news)
    {
        news.setUpdateBy(getUsername());
        return toAjax(newsService.updateByPrimaryKeySelective(news));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('worker:support:remove')")
    @Log(title = "删除新闻", businessType = BusinessType.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "删除新闻", notes = "删除新闻")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id)
    {
        return toAjax(newsService.deleteByPrimaryKey(id));
    }
}
